#this should become a importable tool for easily transforming hoster-urls into a direct media-link

debug=1
dstr=""

import os, glob, imp
modules = {}
#folder=xbmc.translatePath(os.getcwd()+'/../../hoster/') #os.path.join
folder=xbmc.translatePath('Q:\\plugins\\hoster')
for bp in os.listdir(folder):
	path=folder+bp
	name, ext = os.path.splitext(bp)
	if debug > 0: print dstr+"*path: *"+path+" bp: "+bp+" name: "+name
	if name[0]!='_' and ext==".py" and os.path.isfile(path):
		modules[name] = imp.load_source(name, bp, open(path,'r'))

if debug > 0: print dstr+"modules: "+ modules


#options: forcehoster, referrer(including nohistory-option)
def get_verion()
	#or look at the filename
	return 1
	
def get_http(url):
	BASE_PATH = os.getcwd().replace( ";", "" )
	BASE_DATA_PATH = os.path.join( xbmc.translatePath("special://profile/plugin_data/hoster"), "oracle" )
	
	cookiefile = os.path.join(BASE_DATA_PATH,'oracle.lwp')
	cj = cookielib.LWPCookieJar()
	if os.path.isfile(cookiefile):
		cj.load(cookiefile)
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
	urllib2.install_opener(opener)
	#cj.clear()
	
	req = urllib2.Request(url)
	req.add_header('Referer',url)
	req.add_header('Host',re.compile('^http://([^/])/.*)').findall(url)[0] )
	print re.compile('^http://([^/])/.*)').findall(url)[0]
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5')
	req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
	req.add_header('Accept-Language', 'de-de,de;q=0.5')
	req.add_header('Accept-Encoding', 'gzip,deflate')
	req.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
	req.add_header('Keep-Alive', '300')
	req.add_header('Connection', 'keep-alive')
	req.add_header('Cache-Control', 'max-age=0')

	return req

def resolve(url):
	response = urllib2.urlopen(get_http(url))
	return response.geturl()

def get_page(url):
	response = urllib2.urlopen(get_http(url))
	page=response.read()
	response.close()
	page=page.lower()
	return page

	
def get_link_by_get_link(array):
	links=[]
	for method,args in array:
		links.append((method,method(in args)))
	return links
	
	
def get_link_by_url(url,options):
	return #status,link,image,title,filename,captcha,hoster	#return item1

def get_link_by_verification(url,code,options):
	return #return item1	
	
def get_links_by_url(url,options):
	return #array of return item1

def get_links_by_verification(url,code,options):
	return #array of return item1

def get_links_by_page_source(source,options):
	
	
	return #array of return item1

def get_links_by_page_url(url,options):
	return get_links_by_page_source(page=get_page(url).lower(),options)
	

def get_historys():#optional: hoster
	return #array of return item1
	
def do_history():#return item1 
	return #status
	
def del_history():#return item1
	return #status

def del_historys():
	return #status
	
	
def get_downloads():#optional: hoster
	return #array of return item1

def do_download():#return item1
	return #return item1||handle
	
def del_download():#return item1
	return #status

def del_downloads():
	return #status
	

def get_connected_by_url(url,options):
	return #array of conneced type (eg: playlist, related, other videos from user) each containing an array of (url,title,filename,hoster)	#return item2

def get_connected_by_verification(url,options):
	return #return item2
	
def get_connectedlink_by_url(url,options):
	return #array of conneced type (eg: playlist, related, other videos from user) each containing an array of return item1	#return item3

def get_connectedlink_by_verification(url,options):
	return #return item3